package cn.lr.basic.service.impl;

import cn.lr.basic.mapper.BaseMapper;
import cn.lr.basic.query.BaseQuery;
import cn.lr.basic.service.IBaseService;
import cn.lr.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    BaseMapper<T> mapper;

    @Override
    public void save(T t) {
        mapper.save(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        mapper.update(t);
    }

    @Override
    @Transactional
    public void remove(Long id) {
        mapper.remove(id);
    }

    @Override
    public List<T> loadAll() {
        return mapper.loadAll();
    }

    @Override
    public T loadById(Long id) {
        return mapper.loadById(id);
    }

    @Override
    public PageList<T> queryPage(BaseQuery query) {
        //先查询总数
        Long total = mapper.queryCount(query);
        //如果总数为零，直接返回一个PageList对象
        if(total == 0) {
            return new PageList<T>();
        }
        // 再查询每页的数据
        List<T> rows = mapper.queryData(query);
        //将总数和每页的数据返回
        return new PageList<T>(total,rows);
    }

    @Override
    @Transactional
    public void batchRemove(List<Long> ids) {
        mapper.batchRemove(ids);
    }
}
